package com.csu54sher.basereservation.service.impl;

import com.csu54sher.basereservation.constant.ExceptionConstant;
import com.csu54sher.basereservation.constant.StatusConstant;
import com.csu54sher.basereservation.dto.UserDTO;
import com.csu54sher.basereservation.dto.UserLoginDTO;
import com.csu54sher.basereservation.dto.UserPasswordDTO;
import com.csu54sher.basereservation.dto.UserWithoutPasswordDTO;
import com.csu54sher.basereservation.entity.College;
import com.csu54sher.basereservation.entity.User;
import com.csu54sher.basereservation.exception.AccountExistException;
import com.csu54sher.basereservation.exception.NoAccountException;
import com.csu54sher.basereservation.exception.NoCollegeException;
import com.csu54sher.basereservation.exception.WrongPasswordException;
import com.csu54sher.basereservation.mapper.UserMapper;
import com.csu54sher.basereservation.service.ICollegeService;
import com.csu54sher.basereservation.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csu54sher.basereservation.util.BcryptUtil;
import com.csu54sher.basereservation.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author csu54sher
 * @since 2025-10-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private ICollegeService collegeService;

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        // 查询用户
        User user = query().eq("id_number",userLoginDTO.getIdNumber()).one();//学号/工号唯一，故用户也唯一
        if (user== null){
            throw new NoAccountException(ExceptionConstant.NO_ACCOUNT_EXCEPTION);
        }

        // 验证密码
        if(!BcryptUtil.checkPassword(userLoginDTO.getPassword(), user.getPassword())){
            throw new WrongPasswordException(ExceptionConstant.PASSWORD_WRONG_EXCEPTION);
        }

        return user;

    }

    @Override
    public void register(UserDTO userDTO) {
        User user = query().eq("id_number", userDTO.getIdNumber()).one();
        if (user != null){
            throw new AccountExistException(ExceptionConstant.USER_EXIST_EXCEPTION);
        }

        // 添加空值检查
        College college = collegeService
                .query()
                .eq("name", userDTO.getCollege())
                .one();

        if (college == null) {
            throw new NoCollegeException(ExceptionConstant.NO_COLLEGE_EXCEPTION);
        }

        Integer collegeId = college.getId();

        User newUser = new User();
        BeanUtils.copyProperties(userDTO, newUser);
        newUser.setCollegeId(collegeId);
        newUser.setPassword(BcryptUtil.encrypt(userDTO.getPassword()));
        newUser.setIsAdmin(StatusConstant.IS_NOT_ADMIN);
        save(newUser);
    }


    @Override
    public void updateUserInfo(UserWithoutPasswordDTO userWithoutPasswordDTO, String currentId) {
        User user = query().eq("id_number", currentId).one();
        Integer collegeId = collegeService.query().eq("name", userWithoutPasswordDTO.getCollege()).one().getId();
        user.setName(userWithoutPasswordDTO.getName());
        user.setNickname(userWithoutPasswordDTO.getNickname());
        user.setIdNumber(userWithoutPasswordDTO.getIdNumber());
        user.setCollegeId(collegeId);
        user.setDepartment(userWithoutPasswordDTO.getDepartment());
        user.setPhoneNumber(userWithoutPasswordDTO.getPhoneNumber());
        updateById(user);
    }

    @Override
    public void updateUserPassword(UserPasswordDTO userDTO, String currentId) {
        User user = query().eq("id_number", currentId).one();
        if (user == null) {
            throw new NoAccountException(ExceptionConstant.NO_ACCOUNT_EXCEPTION);
        }
        if(!BcryptUtil.checkPassword(userDTO.getOldPassword(), user.getPassword())){
            throw new WrongPasswordException(ExceptionConstant.PASSWORD_WRONG_EXCEPTION);
        }
        user.setPassword(BcryptUtil.encrypt(userDTO.getNewPassword()));
        updateById(user);
    }
}
